<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms;
use app\common\model\anchor\Auit;
use app\common\model\real\Auth;

class Apply extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = "*";

    public function anchor_info()
    {
        $this->success("主播认证信息", Auit::info($this->auth->id));
    }

    public function real_info()
    {
        $this->success("实名认证信息", Auth::info($this->auth->id));
    }

    /*
     * 主播申请
     * */
    public function anchor()
    {
        $params = $this->request->param();
        $params['user_id'] = $this->auth->id;
        if (empty($params['kuaishou']) && empty($params['douyin'])) {
            $this->error("抖音号和快手号至少填写一项");
        }

        try {
            $result = Auit::apply($params);
        } catch (\Exception $exception) {
            $this->error($exception->getMessage());
        }
        if (!$result) {
            $this->error("提交审核失败");
        }
        $this->success("提交审核成功", $result);
    }


    /**
     * 实名认证
     * */
    public function auth()
    {

        $params = $this->request->param();
        $params['user_id'] = $this->auth->id;
        if ($this->auth->group_id != 1) {
            $this->error("当前用户身份有误");
        }
        $a = "/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/";
        if(!preg_match($a,$params['id_number'])){
            $this->error('身份证格式错误');
        }
        if(empty($params['code'])){
            $this->error("验证码不能为空");
        }
        $ret = Sms::check($params['phone'], $params['code'], 'real_auth');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        try {
            $result = Auth::apply($params);
        } catch (\Exception $exception) {
            $this->error($exception->getMessage());
        }

        if (!$result) {
            $this->error("提交审核失败");
        }
        $this->success("提交审核成功", $result);
    }
}